package net.kts.attraction.subway;

import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import net.kts.attraction.subway.positon.SubwayPositionDaoSQLite;
import net.kts.attraction.subway.positon.model.SubwayPosition;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

public class AttractionToSubway extends Activity {

	private LocationManager locationManager = null;
	private LocationListener locationListener = null;
	private boolean locationFlag = false;
	private int providerFlag = 1; //1 gps 2 network
	private Timer timer = null;
	
	
	private SubwayPositionDaoSQLite positionDaoSQLite = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		locationListenerInit();
//		timerInit();
		
		if(this.positionDaoSQLite != null) {
			this.positionDaoSQLite.close();
		}
		this.positionDaoSQLite = new SubwayPositionDaoSQLite(this, "kts_test.db", null, 1);
		
		
		
		this.locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		
		this.locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 1, this.locationListener);
		
		
		
	}
	
	
	private void timerInit() {
		Timer timer = new Timer();
		timer.schedule(new TimerTask() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				
				if(locationFlag == false) {
					if(providerFlag == 1) {
						locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
					}
					else {
						locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
					}
					locationFlag = false;
				}
				
				
			}
		}, new Date(), 3 * 1000);
	}
	private void locationListenerInit() {
		
		this.locationListener = new LocationListener() {
			
			@Override
			public void onStatusChanged(String provider, int status, Bundle extras) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProviderEnabled(String provider) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProviderDisabled(String provider) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onLocationChanged(Location location) {
				// TODO Auto-generated method stub
				// ���⼭ ������!~
//				Toast.makeText(getBaseContext(), "New location latitude ["  +
//			    		lat + "] longitude [" + lng + "]", Toast.LENGTH_SHORT).show();
				changeLocation(location);
			}
		};
		
	}


	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this.locationListener);
		
	}
	
	public void changeLocation(Location location) {
		this.locationFlag = true;
		
		List<SubwayPosition> positionList = this.positionDaoSQLite.getlocationList(location, -1);
		
		SubwayPosition position = this.getNearPosition(location, positionList);
		
		String temp = "";
		temp += "location lat : " + location.getLatitude();
		temp += "location lng : " + location.getLongitude();
		if(position != null) {
			temp += "subway lat : " + position.getLatitude();
			temp += "subway lng : " + position.getLongitude();
			temp += " distance : " + position.getDistance();
		}
		else {
			temp += " Subway NotFound!!";
		}
		
		Toast.makeText(getBaseContext(), temp, Toast.LENGTH_SHORT).show();
	}
	
	private SubwayPosition getNearPosition(Location location, List<SubwayPosition> positionList) {
		
		SubwayPosition position = null;
		SubwayPosition temp = null;
		double distance = 0;
		
		if(positionList == null) {
			return position;
		}
		
		if(positionList.size() == 1) {
			return positionList.get(0);
		}
		
		for(int indexI = 0 ; indexI < positionList.size() ; ++indexI) {
			temp = positionList.get(indexI);
			distance = location.distanceTo(temp);
			temp.setDistance(distance);
			
			
			if(position == null) {
				position = temp;
			}
			else {
				if(position.getDistance() > temp.getDistance()) {
					position = temp;
				}
			}
			
		}
		
 		
		return position;
		
	}


	
	
	
	
	
}
